<?php

/**
 * This is the model class for table "book".
 *
 * The followings are the available columns in table 'book':
 * @property string $book_id
 * @property string $name
 * @property string $author
 * @property string $description
 * @property string $picture_url
 * @property string $isbn
 * @property integer $deleted
 * @property string $date_updated
 * @property string $date_created
 *
 * The followings are the available model relations:
 * @property Tag[] $tags
 * @property Land[] $lands
 * @property Member[] $members
 */
class BaseBook extends CActiveRecord {

    /**
     * Returns the static model of the specified AR class.
     * @param string $className active record class name.
     * @return Book the static model class
     */
    public static function model($className='Book') {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName() {
        return 'book';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules() {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('name, author, description, picture_url, isbn', 'required'),
            array('deleted, date_updated, date_created, image', 'safe'),
            array('deleted', 'numerical', 'integerOnly' => true),
            array('name, picture_url', 'length', 'max' => 128),
            array('author', 'length', 'max' => 64),
            array('isbn', 'length', 'max' => 12),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('book_id, name, author, description, picture_url, isbn, deleted, date_updated, date_created', 'safe', 'on' => 'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations() {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
            'tags' => array(self::MANY_MANY, 'Tag', 'book_has_tag(book_id, tag_id)'),
            'lands' => array(self::HAS_MANY, 'Land', 'book_id'),
            'members' => array(self::MANY_MANY, 'Member', 'land(book_id, member_id)'),
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
        return array(
            'book_id' => 'Könyv sorszáma',
            'name' => 'Név',
            'author' => 'Szerző',
            'description' => 'Leírás',
            'picture_url' => 'Kép Url',
            'isbn' => 'Isbn',
            'deleted' => 'Deleted',
            'date_updated' => 'Módosítás dátuma',
            'date_created' => 'Létrehozás dátuma',
            'tagsString' => 'Címkék',
            'lands' => 'Kölcsönzések',
        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search() {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria = new CDbCriteria;

        $criteria->compare('book_id', $this->book_id, true);
        $criteria->compare('name', $this->name, true);
        $criteria->compare('author', $this->author, true);
        $criteria->compare('description', $this->description, true);
        $criteria->compare('picture_url', $this->picture_url, true);
        $criteria->compare('isbn', $this->isbn, true);
        $criteria->compare('deleted', $this->deleted);
        $criteria->compare('date_updated', $this->date_updated, true);
        $criteria->compare('date_created', $this->date_created, true);

        return new CActiveDataProvider($this, array(
                    'criteria' => $criteria,
                ));
    }
}